/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts.action;

import com.myapp.service.bo.ActionResultBO;
import com.myapp.bo.Extensiontabs;
import com.myapp.dbdao.DesInvestDBDAO;
import com.myapp.struts.form.ReportForm;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jxls.transformer.XLSTransformer;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author toint
 */
public class ReportAction extends Action {

    private static final String SUCCESS = "success";
    private static final String FAILED = "failed";
    private static final String DOWNLOAD = "download";
    
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest req, HttpServletResponse response) throws Exception {
        response.setContentType("charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        ReportForm frm = (ReportForm) form;
        String eventor = frm.getName();
        String privinces = frm.getProvince();
        String fromDate = frm.getFromDate();
        String toDate = frm.getToDate();
        String templateFilename = "";
        String reportFilename = "";
        Map beans = new HashMap();
        templateFilename = "/jsp/template/DesInvestar.xls";
        reportFilename = "/report_out/DesInvestar.xls";
        beans.put("eventor", eventor);
        beans.put("privinces", privinces);
        beans.put("fromDate", fromDate);
        beans.put("toDate", toDate);
        DesInvestDBDAO dbDao = new DesInvestDBDAO();
        List<Extensiontabs> allExtensionTab = dbDao.getAllExtensionTab(frm);
        beans.put("allExtensionTab", allExtensionTab);
        XLSTransformer transformer = new XLSTransformer();
        if (templateFilename != null && reportFilename != null) {
            transformer.transformXLS(req.getRealPath(templateFilename), beans, req.getRealPath(reportFilename));
            req.setAttribute("exportPath", req.getContextPath() + reportFilename);
        }
        return mapping.findForward(DOWNLOAD);
    }
}
